package com.my.hzb.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import com.alibaba.dubbo.config.annotation.Service;
import com.my.hzb.common.bean.ResultBean;
import com.my.hzb.util.HttpHelper;
import com.my.hzb.common.util.JsonUtil;

@Service
public class TestServiceImpl implements TestService {

	private static final Logger log = LogManager.getLogger(TestServiceImpl.class);
	
	private static final String userurl = "http://www.houzhangbo.com/";
	private static final String prourl = "http://pro.houzhangbo.com/";
	private static final String orderurl = "http://order.houzhangbo.com/";
	
	
	public String testt(Integer concurrent,final Integer sum, final String name){
		final List<Long> list = new ArrayList<Long>();
		for(int m=0;m<concurrent;m++){
			new Thread(new Runnable() {
				public void run() {
					for(int i=0;i<sum;i++){
						long start = System.currentTimeMillis();
						if("1".equals(name)){
							testSinger();
						}else if("2".equals(name)){
							testLogin();
						}else if("5".equals(name)){
							testAddOrder();
						}else if("6".equals(name)){
							testLogin();
						}
						list.add(System.currentTimeMillis()-start);
					}
				}
			}).start();
		}
		int summ = concurrent*sum;
		while(list.size()<summ){
			if(list.size()>=summ){
				System.out.println("*******************************");
				return sort(list);
			}
		}
		return "生成报文失败";
	}
	public void testAddAndPay(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("userName", "hzb");
		map.put("adsId", "1");
		map.put("productId", "2");
		map.put("orderPrice", "120");
		map.put("orderNumber", "3");
		String result = http(orderurl+"order/addOrder",map);
		ResultBean re = JsonUtil.g.fromJson(result, ResultBean.class);
		if(null!=re&&null!=re.getRespCode()&&"0000".equals(re.getRespCode()))
		{
			String orderNo = re.getReturnStr().substring(re.getReturnStr().indexOf("orderNo")+7,re.getReturnStr().length());
			map.put("orderNo", orderNo);
			String payorderStr = HttpHelper.doPost(orderurl+"order/payOrder", map);
			log.info("orderNo="+orderNo+",支付结果="+payorderStr);
		}
	}
	public void testAddOrder(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("userName", "hzb");
		map.put("adsId", "1");
		map.put("productId", "2");
		map.put("orderPrice", "120");
		map.put("orderNumber", "3");
		String result = http(orderurl+"order/addOrder",map);
		log.info("提交订单result="+result);
	}
	public void testLogin(){
		Map<String,String> map = new HashMap<String,String>();
		map.put("userName", getStr(8));
		map.put("userPwd", getStr(4));
		String result = http(userurl+"user/login",map);
		System.out.println("result="+result);
	}
	public void testSinger(){
		long start = System.currentTimeMillis();
		Map<String,String> map = new HashMap<String,String>();
		map.put("userName", getStr(8));
		map.put("userPwd", getStr(4));
		String result = http(userurl+"user/reginer",map);
//		System.out.println("result="+result);
		long amount = System.currentTimeMillis() - start;
	}
	
	public String http(String url,Map<String,String> map){
		String str = HttpHelper.doPost(url, map);
		log.info("请求的url="+url+",params="+map+",结果是="+str);
		System.out.println("请求的url="+url+",params="+map+",结果是="+str);
		return str;
	}
	
	public static String getStr(int length){
		String chars = "abcdefghijklmnopqrstuvwxyz";
		String str = ""; 
		for(int i=0;i<length;i++){
			str = str + String.valueOf(chars.charAt((int)(Math.random() * 26)));
		}
		return str;
	}
	
	public List<Bean> getList(){
		List<Bean> list = new ArrayList<Bean>();
		list.add(new Bean("1","注册"));
		list.add(new Bean("2","登录"));
		list.add(new Bean("3","查询地址"));
		list.add(new Bean("4","查询商品"));
		list.add(new Bean("5","提交订单"));
		list.add(new Bean("6","提交、支付订单"));
		return list;
	}
	
	public class Bean{
		public Bean(String val,String name){this.name=name;this.val=val;}
		private String name;
		private String val;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getVal() {
			return val;
		}
		public void setVal(String val) {
			this.val = val;
		}
		
	}
	public static String sort(List<Long> list){
		Collections.sort(list);
		System.out.println("响应时间最短："+list.get(0));
		System.out.println("响应时间最长："+list.get(list.size()-1));
		System.out.println("响应时间平均："+getAvg(list));
		return "响应时间最短："+list.get(0)+","+"响应时间最长："+list.get(list.size()-1)+","+"响应时间平均："+getAvg(list);
	}
	public static Long getAvg(List<Long> list){
		Long am = 0L;
		for(Long l:list){
			am = am + l;
		}
		return am/list.size();
	}
	public static void main(String[] args) {
		Map<String,String> map = new HashMap<String,String>();
		map.put("userName", getStr(8));
		map.put("userPwd", getStr(4));
		String str = HttpHelper.doPost(userurl+"user/reginer", map);
		System.out.println(str);
	}
	@Override
	public String postman(String reqUrl, String reqMethod) {
		Map<String,String> map = new HashMap<String,String>();
		String str = HttpHelper.doGet(reqUrl);
		return str;
	}
	
	public static String getName(Integer n){
		if(1==n){
			return "注册";
		}else if(2==n){
			return "登录";
		}else if(3==n){
			return "查询地址";
		}else if(4==n){
			return "查询商品";
		}else if(5==n){
			return "提交订单";
		}else if(6==n){
			return "提交、支付订单";
		}
		return "";
	}
}
